1931D - Divisible Pairs - CodeForces Solution


combinatorics implementation math number theory

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <string>

using namespace std;

#define ll long long int
#define pb push_back
#define mp make_pair
#define vi vector<ll>
#define vs vector<string>
#define v(t) vector<t>
#define mii map<ll, ll>
#define umii unordered_map<ll, ll>
#define si set<ll>
#define usi unordered_set<ll>
#define pq priority_queue<ll>
#define pqs priority_queue<ll, vi, greater<ll>>
#define all(v) v.begin(), v.end()
#define ff first
#define ss second
#define loop(i, a, b) for (ll i = a; i < b; i++)
#define loopr(i, a, b) for (ll i = a; i > b; i--)
#define print(v) for (auto x : v) cout << x << " ";
#define endl "\n"
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);

#define MOD 1000000007
#define PI 3.1415926535897932384626
ll gcd(ll a,ll b) { if (b==0) return a; return gcd(b, a%b); }
ll lcm(ll a,ll b) { return a/gcd(a,b)*b; }

void solve() {
    ll n, x, y, a, a_x, a_y, b_x, b_y, ans = 0;
    cin >> n >> x >> y;
    map<pair<int, int>, int> count;
    loop(i, 0, n) {
        cin >> a;
        a_x = a % x, a_y = a % y;
        if (a_x == 0) b_x = 0; else b_x = x - a_x;
        if (a_y == 0) b_y = 0; else b_y = a_y;
        ans += count[make_pair(b_x, b_y)];
        count[make_pair(a_x, a_y)]++;
    }

    cout << ans << endl;
}

int main() {
    fast

    #ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif

    ll t;
    cin >> t;

    while (t--) {
        solve();
    }

    // solve();

    return 0;
}


Comments

Submit
0 Comments
More Questions

368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings